<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="path" value="${pageContext.request.contextPath }"></c:set>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${path }/common/plugins/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css">
<link href="${path }/common/plugins/zTree/css/demo.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="${path }/js/jquery-2.0.0.min.js"></script>
<!--树结构脚本-->
<script src="${path }/common/plugins/zTree/jquery.ztree.all-3.5.min.js"></script>
</head>
<script type="text/javascript">
	var nodes = ${zNodes};
	var zsetting = {
		data : {
			key : {
				url : "u"
			},
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : zonClick
		}
	};
	function zonClick(event, treeId, treeNode, clickFlag) {
		onClick(event, treeId, treeNode, clickFlag);
	}
	
	$(document).ready(function() {
		if(typeof setting !="undefined"){
			var setting_s = $.extend(zsetting,setting);
			var obj = $.fn.zTree.init($("#treeWay"), setting_s, nodes);
		}else{
			var obj = $.fn.zTree.init($("#treeWay"), zsetting, nodes);
		}
	});
	/**
	 *性能topN页面树搜索
	 */
	function selectNodes(){
		var str = $("#searchResource").val();
		//检索条件为空时重新初始化树
		if(str==null||$.trim(str)==''){
			var setting_s = $.extend(zsetting,setting);
			$.fn.zTree.init($("#treeWay"), setting_s, nodes);
			return;
		}
		//匹配到的设备节点
		var getNodes = [];
		for(var i=0;i<nodes.length;i++){
			if(nodes[i].id.indexOf("ins_")>=0&&nodes[i].name.indexOf(str)>=0){
				getNodes.push(nodes[i]);
			}
		}
		
		var searchNodes=[]
		//防止树节点重复添加
		var treeIdstr = "";
		for(var i=0;i<getNodes.length;i++){
			//查找设备节点的所有父节点
			var tmpNodes=[];
			getAllParentNode(tmpNodes,getNodes[i]);
			for(var j=0;j<tmpNodes.length;j++){
				if(treeIdstr.indexOf(tmpNodes[j].id+",")<0||tmpNodes[j].id.indexOf("ins_")>=0){
					treeIdstr+=tmpNodes[j].id+",";
					searchNodes.push(tmpNodes[j])
				}
			}
		}
		
		var setting_s = $.extend(zsetting,setting);
		var zTree = $.fn.zTree.init($("#treeWay"), setting_s, searchNodes);
		zTree.expandAll(true);
	}
	/**
	 *获取所有父节点包含自身
	 */
	function getAllParentNode(tmpNodes,node){
		tmpNodes.push(node);
		var pNode=getParentNode(node);
		if(pNode!=null){
			getAllParentNode(tmpNodes,pNode);
		}
	}
	/**
	*获取父节点
	*/
	function getParentNode(node){
		var pid=node.pId;
		if(pid==null||pid==''){
			return null;
		}
		for(var i=0;i<nodes.length;i++){
			if(pid==nodes[i].id){
				return nodes[i];
			}
		}
	}
	/**
	 * 获取zTree选中的节点
	 * @returns treeNode
	 */
	function getSelectNode(){
		var treeObj = $.fn.zTree.getZTreeObj("treeWay");
		var nodes = treeObj.getSelectedNodes();
		if(nodes!=null&&nodes.length>0){
			return nodes[0];
		}else{
			return null;
		}
	}
	/**
	 * 获取选中节点父节点
	 * @return treeNode
	 */
	function getSelectedNodeParent(){
		var parentNode=null;
		var selectedNode = getSelectNode();
		if(selectedNode!=null){
			parentNode = selectedNode.getParentNode();
		}
		return parentNode;
	}
	/**
	 * 级联获取所有子孙节点id，以逗号分隔
	 * @param zNode zTree节点
	 * @param strIds id1,id2,id3,....
	 */
	function getSelectNodeChildrensId(zNode){
		var strIds='';
		if(zNode.length!=0&&zNode.children!=null){
			for(var i=0;i<zNode.children.length;i++){
				strIds=strIds+zNode.children[i].id+",";
				strIds=strIds+getSelectNodeChildrensId(zNode.children[i]);
			}
		}
		return strIds;
	}
	/**
	 * 级联获取所有子孙节点
	 * @param zNode zTree节点
	 * @return zNode数组
	 */
	function getSelectNodeChildrens(zNode){
		var arrNode=new Array();
		if(zNode.length!=0&&zNode.children!=null){
			for(var i=0;i<zNode.children.length;i++){
				arrNode.push(zNode.children[i]);
				var arrSonNode = getSelectNodeChildrens(zNode.children[i]);
				if(arrSonNode.length!=0){
					arrNode = arrNode.concat(arrSonNode);
				}
			}
		}
		return arrNode;
	}
	/**
	 * 级联获取所有子孙节点，对应的子孙节点id
	 * @param zNode zTree节点
	 * @return map<父节点id，子孙节点id以逗号分隔>
	 */
	function getALLNodeChildrensId(zNode){
		 var arrNodes = getSelectNodeChildrens(zNode);
		 arrNodes.push(zNode);
		 var map = {};
		 for(var i=0;i<arrNodes.length;i++){
			var sonIds = getSelectNodeChildrensId(arrNodes[i]);
			if(sonIds!=''){
				sonIds = sonIds.substring(0,sonIds.length-1);
			}
			map[arrNodes[i].id]= sonIds;
		 }
		 return map;
	}
</script>
<body>
	<!-- <h1>欢迎你！！！</h1> -->
	<div>
		<ul id="treeWay" class="ztree"></ul>
	</div>
</body>
</html>